package com.mkvld.appietijdenv2.fragments;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;

import com.mkvld.appietijdenv2.R;
import com.mkvld.appietijdenv2.adapters.AdapInfoFrags;

import android.support.v4.app.DialogFragment;
import android.app.Dialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FragInfoV9 extends DialogFragment {
	private static final String TAG = "FragInfoV9";
	ViewPager viewPager;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.pager_with_strip, null, false);
		viewPager = (ViewPager) v.findViewById(R.id.pager);
		loadPageNames("http://at.remcomokveld.nl/info.pages.php");
		return v;
	}
	
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		Dialog d = super.onCreateDialog(savedInstanceState);
		d.setTitle(R.string.as_info);
		return d;
	}
	
	private static String convertStreamToString(InputStream is) {
        /*
         * To convert the InputStream to String we use the BufferedReader.readLine()
         * method. We iterate until the BufferedReader return null which means
         * there's no more data to read. Each line will appended to a StringBuilder
         * and returned as String.
         */
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
 
        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
 
    /* This is a test function which will connects to a given
     * rest service and prints it's response to Android Log with
     * labels "Praeda".
     */
    public void loadPageNames(String url)
    {
    	new AsyncTask<String, Void, String[]>() {

			@Override
			protected String[] doInBackground(String... params) {
				String[] pages = null;
				HttpClient httpclient = new DefaultHttpClient();
				 
		        // Prepare a request object
		        HttpGet httpget = new HttpGet(params[0]); 
		 
		        // Execute the request
		        HttpResponse response;
		        try {
		            response = httpclient.execute(httpget);
		            // Examine the response status
		            Log.i("Praeda",response.getStatusLine().toString());
		 
		            // Get hold of the response entity
		            HttpEntity entity = response.getEntity();
		            // If the response does not enclose an entity, there is no need
		            // to worry about connection release
		 
		            if (entity != null) {
		 
		                // A Simple JSON Response Read
		                InputStream instream = entity.getContent();
		                String result= convertStreamToString(instream);
		                Log.i("Praeda",result);
		 
		                // A Simple JSONObject Creation
		                JSONArray json=new JSONArray(result);
		                pages = new String[json.length()];
		                for (int i = 0; i < json.length(); i++) {
		                	pages[i] = json.getString(i);
		                }
		 
		                // Closing the input stream will trigger connection release
		                instream.close();
		            }
		 
		 
		        } catch (Exception e) {
		            Log.e(TAG, "getting page titles failed wih reason: "+e.getLocalizedMessage());
		        }
		        return pages;
			}
			
			protected void onPostExecute(String[] result) {
				viewPager.setAdapter(new AdapInfoFrags(getChildFragmentManager(), getActivity(), result));
			}
    		
    	}.execute(url);
    }
}
